package com.xbongbong.saas.util;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.domain.entity.UpdateDataValueEntity;
import com.xbongbong.saas.domain.entity.AssembleProductEntity;
import com.xbongbong.saas.domain.entity.BomProductEntity;
import com.xbongbong.saas.domain.entity.ContractProductEntity;
import com.xbongbong.saas.domain.entity.InstockProductEntity;
import com.xbongbong.saas.domain.entity.InventoryProductEntity;
import com.xbongbong.saas.domain.entity.OpportunityProductEntity;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderProductEntity;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.domain.entity.QuotationProductEntity;
import com.xbongbong.saas.domain.entity.ReturnedPurchaseProductEntity;
import com.xbongbong.saas.domain.entity.SupplierProductEntity;
import com.xbongbong.saas.domain.entity.TransferProductEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 描述信息
 *
 * @ClassName StandardizeData
 * @Author 梁鲁江
 * 创建时间 2022/3/7 15:35
 * 修改时间  2022/3/7 15:35
 **/
public class StandardizeDataUtil {
    private static final Logger LOG = LoggerFactory.getLogger(StandardizeDataUtil.class);

    /**
     * 标准化非String的数据类型
     * @param paasFormDataEntity
     */
    public static void standardizeNotString(PaasFormDataEntity paasFormDataEntity) {
        if(paasFormDataEntity == null){
            return;
        }
        JSONObject json = paasFormDataEntity.getData();
        standardizeJson(paasFormDataEntity.getCorpid(), json);
        paasFormDataEntity.setData(json);
    }

    public static void standardizeNotString(PaasFormSubDataEntity paasFormDataEntity) {
        if(paasFormDataEntity == null){
            return;
        }
        JSONObject json = paasFormDataEntity.getData();
        standardizeJson(paasFormDataEntity.getCorpid(), json);
        paasFormDataEntity.setData(json);
    }

    public static void standardizeNotString(ContractProductEntity contractProductEntity) {
        if(Objects.isNull(contractProductEntity)){
            return;
        }
        JSONObject json = contractProductEntity.getData();
        standardizeJson(contractProductEntity.getCorpid(), json);
        contractProductEntity.setData(json);
    }

    public static void standardizeNotString(OpportunityProductEntity opportunityProductEntity) {
        if(Objects.isNull(opportunityProductEntity)){
            return;
        }
        JSONObject json = opportunityProductEntity.getData();
        standardizeJson(opportunityProductEntity.getCorpid(), json);
        opportunityProductEntity.setData(json);
    }

    public static void standardizeNotString(QuotationProductEntity quotationProductEntity) {
        if(Objects.isNull(quotationProductEntity)){
            return;
        }
        JSONObject json = quotationProductEntity.getData();
        standardizeJson(quotationProductEntity.getCorpid(), json);
        quotationProductEntity.setData(json);
    }

    public static void standardizeNotString(SupplierProductEntity supplierProductEntity) {
        if(Objects.isNull(supplierProductEntity)){
            return;
        }
        JSONObject json = supplierProductEntity.getData();
        standardizeJson(supplierProductEntity.getCorpid(), json);
        supplierProductEntity.setData(json);
    }

    public static void standardizeNotString(PurchaseProductEntity purchaseProductEntity) {
        if(Objects.isNull(purchaseProductEntity)){
            return;
        }
        JSONObject json = purchaseProductEntity.getData();
        standardizeJson(purchaseProductEntity.getCorpid(), json);
        purchaseProductEntity.setData(json);
    }

    public static void standardizeNotString(ReturnedPurchaseProductEntity returnedPurchaseProductEntity) {
        if(Objects.isNull(returnedPurchaseProductEntity)){
            return;
        }
        JSONObject json = returnedPurchaseProductEntity.getData();
        standardizeJson(returnedPurchaseProductEntity.getCorpid(), json);
        returnedPurchaseProductEntity.setData(json);
    }


    public static void standardizeNotString(ProductionOrderProductEntity productionOrderProductEntity) {
        if(Objects.isNull(productionOrderProductEntity)){
            return;
        }
        JSONObject json = productionOrderProductEntity.getData();
        standardizeJson(productionOrderProductEntity.getCorpid(), json);
        productionOrderProductEntity.setData(json);
    }


    public static void standardizeNotString(AssembleProductEntity asembleProductEntity) {
        if(Objects.isNull(asembleProductEntity)){
            return;
        }
        JSONObject json = asembleProductEntity.getData();
        standardizeJson(asembleProductEntity.getCorpid(), json);
        asembleProductEntity.setData(json);
    }

    public static void standardizeNotString(InventoryProductEntity inventoryProductEntity) {
        if(Objects.isNull(inventoryProductEntity)){
            return;
        }
        JSONObject json = inventoryProductEntity.getData();
        standardizeJson(inventoryProductEntity.getCorpid(), json);
        inventoryProductEntity.setData(json);
    }

    public static void standardizeNotString(TransferProductEntity transferProductEntity) {
        if(Objects.isNull(transferProductEntity)){
            return;
        }
        JSONObject json = transferProductEntity.getData();
        standardizeJson(transferProductEntity.getCorpid(), json);
        transferProductEntity.setData(json);
    }

    public static void standardizeNotString(InstockProductEntity instockProductEntity) {
        if(Objects.isNull(instockProductEntity)){
            return;
        }
        JSONObject json = instockProductEntity.getData();
        standardizeJson(instockProductEntity.getCorpid(), json);
        instockProductEntity.setData(json);
    }

    public static void standardizeNotString(OutstockProductEntity outstockProductEntity) {
        if(Objects.isNull(outstockProductEntity)){
            return;
        }
        JSONObject json = outstockProductEntity.getData();
        standardizeJson(outstockProductEntity.getCorpid(), json);
        outstockProductEntity.setData(json);
    }

    public static void standardizeNotString(BomProductEntity bomProductEntity) {
        if(Objects.isNull(bomProductEntity)){
            return;
        }
        JSONObject json = bomProductEntity.getData();
        standardizeJson(bomProductEntity.getCorpid(), json);
        bomProductEntity.setData(json);
    }


    public static void standardize4EntityList(List<? extends PaasFormDataEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (PaasFormDataEntity pasFormDataEntity : entityList) {
            JSONObject json = pasFormDataEntity.getData();
            standardizeJson(pasFormDataEntity.getCorpid(), json);
        }
    }


    public static void standardize4ContractEntityList(List<? extends ContractProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (ContractProductEntity contractProductEntity : entityList) {
            JSONObject json = contractProductEntity.getData();
            standardizeJson(contractProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4SupplierProductEntityList(List<? extends SupplierProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (SupplierProductEntity supplierProductEntity : entityList) {
            JSONObject json = supplierProductEntity.getData();
            standardizeJson(supplierProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4OpportunityProductEntityList(List<? extends OpportunityProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (OpportunityProductEntity opportunityProductEntity : entityList) {
            JSONObject json = opportunityProductEntity.getData();
            standardizeJson(opportunityProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4QuotationProductEntityList(List<? extends QuotationProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (QuotationProductEntity quotationProductEntity : entityList) {
            JSONObject json = quotationProductEntity.getData();
            standardizeJson(quotationProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4PurchaseProductEntityList(List<? extends PurchaseProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (PurchaseProductEntity purchaseProductEntity : entityList) {
            JSONObject json = purchaseProductEntity.getData();
            standardizeJson(purchaseProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4ReturnedPurchaseProductEntityList(List<? extends ReturnedPurchaseProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (ReturnedPurchaseProductEntity returnedPurchaseProductEntity : entityList) {
            JSONObject json = returnedPurchaseProductEntity.getData();
            standardizeJson(returnedPurchaseProductEntity.getCorpid(), json);
        }
    }


    public static void standardize4BomProductEntityList(List<? extends BomProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (BomProductEntity bomProductEntity : entityList) {
            JSONObject json = bomProductEntity.getData();
            standardizeJson(bomProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4ProductionOrderProductEntityList(List<? extends ProductionOrderProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (ProductionOrderProductEntity productionOrderProductEntity : entityList) {
            JSONObject json = productionOrderProductEntity.getData();
            standardizeJson(productionOrderProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4AssembleProductEntityList(List<? extends AssembleProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (AssembleProductEntity assembleProductEntity : entityList) {
            JSONObject json = assembleProductEntity.getData();
            standardizeJson(assembleProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4InventoryProductEntityList(List<? extends InventoryProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (InventoryProductEntity inventoryProductEntity : entityList) {
            JSONObject json = inventoryProductEntity.getData();
            standardizeJson(inventoryProductEntity.getCorpid(), json);
        }
    }


    public static void standardize4TransferProductEntityList(List<? extends TransferProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (TransferProductEntity transferProductEntity : entityList) {
            JSONObject json = transferProductEntity.getData();
            standardizeJson(transferProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4InstockProductEntityList(List<? extends InstockProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (InstockProductEntity instockProductEntity : entityList) {
            instockProductEntity.setUpdateTime(DateTimeUtil.getInt());
            JSONObject json = instockProductEntity.getData();
            standardizeJson(instockProductEntity.getCorpid(), json);
        }
    }

    public static void standardize4OutstockProductEntityList(List<? extends OutstockProductEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (OutstockProductEntity outstockProductEntity : entityList) {
            outstockProductEntity.setUpdateTime(DateTimeUtil.getInt());
            JSONObject json = outstockProductEntity.getData();
            standardizeJson(outstockProductEntity.getCorpid(), json);
        }
    }

    /**
     * 子表单批量标准化
     * @param entityList
     */
    public static void standardize4SubEntityList(List<? extends PaasFormSubDataEntity> entityList) {
        if (CollectionsUtil.isEmpty(entityList)) {
            return;
        }
        for (PaasFormSubDataEntity pasFormDataEntity : entityList) {
            JSONObject json = pasFormDataEntity.getData();
            standardizeJson(pasFormDataEntity.getCorpid(), json);
        }
    }

    public static void standardizeJson(String corpid, JSONObject json) {
        if (Objects.isNull(json) || json.isEmpty()) {
            return;
        }
        try {
            for (String s : json.keySet()) {
                if (s.contains("num_") ) {
                    Object v = json.get(s);
                    if (v instanceof String && StringUtil.isNotEmpty(v.toString())) {
                        String s1 = v.toString();
                        if (s1.contains(".")) {
                            json.put(s, new BigDecimal(s1));
                        } else {
                            json.put(s,Long.valueOf(s1));
                        }
                    }
                } else if (s.contains("date_") || s.contains("long_")) {
                    Object v = json.get(s);
                    if (v instanceof String && StringUtil.isNotEmpty(v.toString())) {
                        json.put(s,Long.valueOf(v.toString()));
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("标准化json数据失败! corpid: " + corpid +  "; json数据: " + json, e);
        }
    }

    public static void standardizeList(List<UpdateDataEntity> updateList) {
        if(CollectionsUtil.isEmpty(updateList)){
            return;
        }
        for (UpdateDataEntity updateDataEntity : updateList) {
            List<UpdateDataValueEntity> list = updateDataEntity.getData();
            if(CollectionsUtil.isEmpty(list)){
                continue;
            }
            for (UpdateDataValueEntity updateDataValueEntity : list) {
                String key = updateDataValueEntity.getKey();
                if(StringUtil.isEmpty(key)){
                    continue;
                }
                try {
                    if(key.contains("num_") ){
                        Object v = updateDataValueEntity.getValue();
                        if(v instanceof String && StringUtil.isNotEmpty(v.toString())){
                            String s1 = v.toString();
                            if(s1.contains(".")){
                                updateDataValueEntity.setValue(new BigDecimal(s1));
                            }else {
                                updateDataValueEntity.setValue(Long.valueOf(s1));
                            }
                        }
                        //TODO  “”只有引号的处理
                    }else if(key.contains("date_") || key.contains("long_")){
                        Object v = updateDataValueEntity.getValue();
                        if(v instanceof String && StringUtil.isNotEmpty(v.toString())){
                            updateDataValueEntity.setValue(Long.valueOf(v.toString()));
                        }
                    }
                } catch (Exception e) {
                    LOG.error("标准化数据失败，updateDataValueEntity：" + updateDataValueEntity, e);
                }
            }
        }
    }

    public static void standardizeListMap(List<Map<String, Object>> updateContractEntity) {
        if(CollectionsUtil.isEmpty(updateContractEntity)){
            return;
        }
        for (Map<String, Object> stringObjectMap : updateContractEntity) {
            try {
                for (String s : stringObjectMap.keySet()) {
                    if(s.contains("num_") ){
                        Object v = stringObjectMap.get(s);
                        if(v instanceof String && StringUtil.isNotEmpty(v.toString())){
                            String s1 = v.toString();
                            if(s1.contains(".")){
                                stringObjectMap.put(s, new BigDecimal(s1));
                            }else {
                                stringObjectMap.put(s,Long.valueOf(s1));
                            }
                        }
                    }else if(s.contains("date_") || s.contains("long_")){
                        Object v = stringObjectMap.get(s);
                        if(v instanceof String && StringUtil.isNotEmpty(v.toString())){
                            stringObjectMap.put(s,Long.valueOf(v.toString()));
                        }
                    }
                }
            } catch (Exception e) {
                LOG.error("标准化数据失败！updateContractEntity: " + updateContractEntity, e);
            }
        }
    }


    public static void standardizeMap(Map<String, Object> param) {
        if (Objects.isNull(param) || param.isEmpty()) {
            return;
        }
        try {
            for (String s : param.keySet()) {
                if (s.contains("num_") ){
                    Object v = param.get(s);
                    if (v instanceof String && StringUtil.isNotEmpty(v.toString())) {
                        String s1 = v.toString();
                        if (s1.contains(".")) {
                            param.put(s, new BigDecimal(s1));
                        } else {
                            param.put(s,Long.valueOf(s1));
                        }
                    }
                } else if (s.contains("date_") || s.contains("long_")) {
                    Object v = param.get(s);
                    if (v instanceof String && StringUtil.isNotEmpty(v.toString())) {
                        param.put(s,Long.valueOf(v.toString()));
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("标准化Map数据失败，Map数据：" + param, e);
        }
    }

    public static void main(String[] args) {
        List<PaasFormDataEntity> paasFormDataList = new ArrayList<>();
        PaasFormDataEntity paasFormData = new PaasFormDataEntity();
        JSONObject data = new JSONObject();
        data.put("num_1", "432");
        data.put("date_23", "654654645");
        data.put("long_43", "34326f4");
        paasFormData.setData(data);
        paasFormDataList.add(paasFormData);
        standardize4EntityList(paasFormDataList);
        LOG.info(paasFormDataList.toString());

        Map<String, Object> params = new HashMap<>(2 << 2);
        params.put("num_43", "");
        params.put("date_34", "432467");
        params.put("long_432", "432432432");
        standardizeMap(params);
        LOG.info(params.toString());
    }
}
